package com.ftg.learn.chapter12;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * collection集合常 用方法
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/2/8 下午2:59
 */
public class LearnCollection {

    public static void main(String[] args) {

        Collection<String> collection = new ArrayList<>();
        System.out.println(collection.size() + "现有元素个数");
        System.out.println(collection.isEmpty());

        collection.add("hello");
        collection.add("hello1");
        collection.add("hello");
        collection.add("world");
        System.out.println(collection.size() + "现有元素个数");
        System.out.println(collection.isEmpty());

        System.out.println(collection.contains("hello1"));

        //collection 遍历 迭代器 Iterator
        //将collection集合转换成为iterator
        Iterator<String> it = collection.iterator();
        System.out.println("迭代器");
        while(it.hasNext()){
            String val = it.next();
            System.out.println(val+"遍历所有值");
        }

        System.out.println("使用foreach增强型");
        for(String str : collection){
            System.out.println(str);
        }

        System.out.println("foreach lambda表达式  方法调用");
        collection.forEach(System.out::println);


//        boolean ist = collection.remove("hello");
//        System.out.println(ist);

//        Collection<String> sub = new ArrayList<>();
//        sub.add("hello");
//        sub.add("world");
//        boolean ist1 = collection.removeAll(sub);
//
//        System.out.println(ist1);

        collection.removeIf(x->x.startsWith("h") && x.endsWith("1"));

        System.out.println("foreach lambda表达式 ");
        collection.forEach(x-> System.out.println(x));

    }

}
